<template>
  <div class="checkout-address">
    <div class="text">
      <template v-if="defaultAddresses.id">
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{defaultAddresses.receiver}}</li>
          <li><span>联系方式：</span>{{defaultAddresses.contact}}</li>
          <li><span>收货地址：</span>{{defaultAddresses.fullLocation+defaultAddresses.address}}</li>
        </ul>
        <a href="javascript:;" @click="openAddressEdit(defaultAddresses)">修改地址</a>
      </template>
      <div class="none" v-else>您需要先添加收货地址才可提交订单。</div>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible=true">切换地址</XtxButton>
      <XtxButton class="btn" @click="openAddressEdit()">添加地址</XtxButton>
    </div>
  </div>
  <!-- 新增和修改 -->
  <AddressEdit ref="addressEdit" @changes="getAddressEdit"></AddressEdit>
  <XtxDialog title="切换收货地址" v-model="dialogVisible">
      <div class="text item" :class="{active:item.id===currId}" v-for="item in addresses" @click="changeAddressId(item)" :key="item.id">
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
        </ul>
      </div>
      <template v-slot:footer>
        <XtxButton type="gray" style="margin-right: 20px" @click="dialogVisible=false">取消</XtxButton>
      <XtxButton type="primary" @click="changeAddress">确认</XtxButton>
      </template>
  </XtxDialog>
</template>
<script>
import { inject, ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
import { editAddress } from '@/api/order'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  props: {
    addresses: {
      type: Array,
      default: () => []
    }
  },
  setup (props, { emit }) {
    const dialogVisible = ref(false)
    const addressEdit = ref(null)
    const openAddressEdit = (val) => {
      addressEdit.value.open(val)
    }
    const defaultAddresses = ref(null)
    defaultAddresses.value = props.addresses.find(item => item.isDefault === 0) || {}

    const getAddressEdit = (data) => {
      // console.log(defaultAddresses, data)
      defaultAddresses.value = { ...data }
    }

    const currId = ref(defaultAddresses.value.id)
    const selectedAddress = ref(null)
    const changeAddressId = (address) => {
      currId.value = address.id
      selectedAddress.value = address
    }
    const changeAddress = async () => {
      dialogVisible.value = false
      // console.log(selectedAddress)
      if (!selectedAddress.value.id) return
      defaultAddresses.value.isDefault = 1
      selectedAddress.value.isDefault = 0
      await editAddress(defaultAddresses.value)
      await editAddress(selectedAddress.value)
      defaultAddresses.value = selectedAddress.value
    }

    const data = inject('data')
    watch(defaultAddresses, ({ id }) => {
      data.addressId = id
    }, { immediate: true })
    return { dialogVisible, addressEdit, openAddressEdit, defaultAddresses, getAddressEdit, changeAddressId, currId, changeAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
